package org.dream.persistence;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.dream.dto.LongTou;

public class IndustryPersistenceImpl {
	public List<String> findIndustryBkmc(boolean calculate) {

		List<String> bkmcs = new ArrayList<String>();
		try {
			Statement stmt = DBUtil.getStatement();
			String sql = "select  bkmc from industry where day=(select MAX(day) from industry) order by jzf desc";
			if (calculate) {
				sql = "select distinct bkmc from stock";
			}

			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				String bkmc = rs.getString("bkmc");
				bkmcs.add(bkmc);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bkmcs;
	}

	public Map<String, List<LongTou>> findLongTous() {
		Map<String, List<LongTou>> map = new LinkedHashMap<String, List<LongTou>>();

		try {
			Statement stmt = DBUtil.getStatement();
			String sql = "select bkmc,lzgp ,count(lzgp) ltcount from industry  where lzgp !='' group by bkmc,lzgp order by bkmc, count(lzgp) desc";

			ResultSet rs = stmt.executeQuery(sql);
			String bkmc = "";
			int i = 0;
			List<LongTou> list = null;
			while (rs.next()) {

				if (!rs.getString("bkmc").equals(bkmc)) {
					bkmc = rs.getString("bkmc");
					i = 0;
					list = new ArrayList<LongTou>();
					map.put(bkmc, list);

				}
				LongTou longTou = new LongTou();
				longTou.setBkmc(bkmc);
				longTou.setLongTou(rs.getString("lzgp"));
				longTou.setCount(rs.getInt("ltcount"));
				list.add(longTou);

				i++;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return map;
	}
}
